import { EquipmentSlot,  GameMode,  Player,  world } from "@minecraft/server";
import { clearEffect, getEquipmentItem, consumeDurability } from "./core";

world.afterEvents.playerBreakBlock.subscribe((event) => {
  const [PLAYER, ITEM] = [event.player, getEquipmentItem(event.player)];
  if (
    PLAYER.getGameMode() === GameMode.survival ||
    PLAYER.getGameMode() === GameMode.adventure
  ) {
    if (ITEM?.hasTag("stonecraft:custom_weapon")) {
      const NEW_ITEM = consumeDurability(ITEM, 2, PLAYER);
      PLAYER?.getComponent("minecraft:equippable")?.setEquipment(
        EquipmentSlot.Mainhand,
        NEW_ITEM
      );
    }
  }
});
world.afterEvents.entityHitEntity.subscribe((event) => {
  const [ENTITY, ITEM] = [
    event.damagingEntity,
    getEquipmentItem(event.damagingEntity),
  ];
  if (ENTITY instanceof Player) {
    if (
      ENTITY.getGameMode() === GameMode.survival ||
      ENTITY.getGameMode() === GameMode.adventure
    ) {
      if (ITEM?.hasTag("stonecraft:custom_weapon")) {
        const NEW_ITEM = consumeDurability(ITEM, 1, ENTITY);
        ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
          EquipmentSlot.Mainhand,
          NEW_ITEM
        );
      }
    }
  } else if (ITEM?.hasTag("stonecraft:custom_weapon")) {
    const NEW_ITEM = consumeDurability(ITEM, 1, ENTITY);
    ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
      EquipmentSlot.Mainhand,
      NEW_ITEM
    );
  }
});

world.afterEvents.playerBreakBlock.subscribe((event) => {
  const [PLAYER, ITEM] = [event.player, getEquipmentItem(event.player)];
  if (
    PLAYER.getGameMode() === GameMode.survival ||
    PLAYER.getGameMode() === GameMode.adventure
  ) {
    if (ITEM?.hasTag("stonecraft:custom_tool")) {
      const NEW_ITEM = consumeDurability(ITEM, 1, PLAYER);
      PLAYER?.getComponent("minecraft:equippable")?.setEquipment(
        EquipmentSlot.Mainhand,
        NEW_ITEM
      );
    }
  }
});
world.afterEvents.entityHitEntity.subscribe((event) => {
  const [ENTITY, ITEM] = [
    event.damagingEntity,
    getEquipmentItem(event.damagingEntity),
  ];
  if (ENTITY instanceof Player) {
    if (
      ENTITY.getGameMode() === GameMode.survival ||
      ENTITY.getGameMode() === GameMode.adventure
    ) {
      if (ITEM?.hasTag("stonecraft:custom_tool")) {
        const NEW_ITEM = consumeDurability(ITEM, 2, ENTITY);
        ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
          EquipmentSlot.Mainhand,
          NEW_ITEM
        );
      }
    }
  } else if (ITEM?.hasTag("stonecraft:custom_tool")) {
    const NEW_ITEM = consumeDurability(ITEM, 2, ENTITY);
    ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
      EquipmentSlot.Mainhand,
      NEW_ITEM
    );
  }
});

/**
 * 监听物品食用事件
 */
world.afterEvents.itemCompleteUse.subscribe((event) => {
  const PLAYER = event.source;
  switch (event.itemStack.typeId) {
    case "stonecraft:stone_apple":
      clearEffect(PLAYER, "bad");
      break;
    case "stonecraft:stone_bread":
      PLAYER.addEffect("resistance", 2000); // 100s 抗性提升
      break;
    case "stonecraft:stone_potato":
      PLAYER.addEffect("speed", 600, { amplifier: 1 }); //30s 速度II
      break;
    case "stonecraft:stone_carrot":
      PLAYER.addEffect("night_vision", 600, { amplifier: 1 }); //30s 夜视II
      break;
    case "stonecraft:stone_melon":
      PLAYER.addEffect("regeneration", 600, { amplifier: 1 }); //30s 生命恢复II
      break;
    case "stonecraft:stone_kelp":
      PLAYER.addEffect("water_breathing", 600); //30s 水下呼吸
      break;
    case "stonecraft:stone_hodgepodge":
      //移除所有负面效果，添加30s的 抗性提升；速度II；夜视II；生命恢复II；水下呼吸
      clearEffect(PLAYER, "bad");
      PLAYER.addEffect("resistance", 2000);
      PLAYER.addEffect("speed", 600, { amplifier: 1 });
      PLAYER.addEffect("night_vision", 600, { amplifier: 1 });
      PLAYER.addEffect("regeneration", 600, { amplifier: 1 });
      PLAYER.addEffect("water_breathing", 600);
      break;
    default:
      break;
  }
});

/**
 * 监听实体击打事件
 */
world.afterEvents.entityHitEntity.subscribe((event) => {
  const [ATTACKER, HITER, ITEM] = [
    event.damagingEntity,
    event.hitEntity,
    event.damagingEntity?.getComponent("minecraft:equippable")?.getEquipment(EquipmentSlot.Mainhand),
  ];
  /**
   * 特殊的石剑-5s失明/5s凋零/生命恢复IV 30刻
   */
  if (ITEM?.getDynamicProperty("stonecraft:blindness")) {
    HITER.addEffect("blindness", 100);
  }
  if (ITEM?.getDynamicProperty("stonecraft:wither")) {
    HITER.addEffect("wither", 100);
  }
  if (ITEM?.getDynamicProperty("stonecraft:regeneration")) {
    HITER.addEffect("regeneration", 30, { amplifier: 3 });
  }
  /**
   * 终极压缩石剑相关：
   * lv1-5s 缓慢X
   * lv2-7s 缓慢X
   * lv3-10s 缓慢X；凋零V、3s 力量V(给自己)
   */
  switch (ITEM?.typeId) {
    case "stonecraft:uc_stone_sword":
      HITER.addEffect("slowness", 100, { amplifier: 4, showParticles: false });
      break;
    case "stonecraft:uc2_stone_sword":
      HITER.addEffect("slowness", 140, { amplifier: 4, showParticles: false });
      break;
    case "stonecraft:uc3_stone_sword":
      HITER.addEffect("slowness", 200, { amplifier: 4, showParticles: false });
      HITER.addEffect("wither", 200, { amplifier: 4, showParticles: false });
      ATTACKER.addEffect("strength", 60, {
        amplifier: 4,
        showParticles: false,
      });
      break;
    default:
      break;
  }
});